time = 6000
window.Websocket = {
  handleFriendLoggedOut: (data) ->
    user = Me.friends_findById data.data.user_id
    if user                # user exists, remove it
      user.destroy()
  ,
  handleFriendPosition: (data) ->
    user = Me.friends_findById data.data.user_id
    if user                # user exists, update its position
      # console.log "user exists"
      user.moveMarker( data.data.latitude, data.data.longitude )
    unless user            # user not exists, create it
      position = {
        latitude: data.data.latitude,
        longitude: data.data.longitude
      }
      $.getJSON('/users/' + data.data.user_id, (data) ->
        # console.log data
        # check if friend exists:
        exists = false
        for friend in Me.friends
          if user.id == friend.id
            exists = true
        unless exists
          Me.friends.push(user)
          user = new MapObjects.Person( data , position )
      )
  ,
  handleReceipt: (data) ->
    if data.type == "error" or data.type == "notice"
      Geo.raiseNotice( data.text, data.type )
    if data.code == -1                                  # logged in on another place. Log out this person
      setTimeout( ->
        Geo.raiseNotice( "Te sacaré de aquí en unos segundos." )
        setTimeout( ->
          window.location = "/"
        , time)
      , time)
    if data.code == 1
      # console.log "recieved friend position"
      Websocket.handleFriendPosition(data)
    if data.code == 2
      # console.log "friend logged out"
      Websocket.handleFriendLoggedOut(data)
  ,
  sendObject: ->
    # doing nothing (setted later)
  ,
  connected: false,
  ws: null,
  recieved_data: (e) ->
    # console.log "Recieved data."
    data = JSON.parse(e.data)
    # console.log data
    setTimeout( ->
      Websocket.handleReceipt data
    , 0)
  closed: ->
    Websocket.connected = false
    console.log "Websocket closed"
    Geo.raiseNotice( "Lo siento. Algo salió mal. Intentaré reconectarme en unos segundos. <a href='/error_explanation' target='_blank'>Más información acerca de este problema</a>." )
    setTimeout( ->
      unless Websocket.connected
        # window.location = "/"
        Websocket.init()
    , time)
  opened: ->
    Websocket.connected = true
    console.log "Websocket opened"
    # Set method sendObject:
    Websocket.sendObject = (object) ->
      # object.token = Cookie.read("user_id")       # no longer required, as we now read token from cookie on websocket directly
      setTimeout( ->
        Websocket.ws.send( JSON.stringify(object) )
      , 0)
    # send token:                                   # (No longer required, this was to initialize token)
    # token = { type:'token' }
    # Websocket.sendObject( token )
    Geo.hideNotice()
  init: ->
    Socket = window.WebSocket
    if window.MozWebSocket
      Socket = window.MozWebSocket

    port = 8081
    Websocket.ws = new Socket( "ws://" + window.location.hostname + ":" + port )
    Websocket.ws.onmessage = Websocket.recieved_data
    Websocket.ws.onclose = Websocket.closed
    Websocket.ws.onopen = Websocket.opened
}